﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using shfa.snomtastic.data.security;


namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eConfigurationSetting : EditBase
    {
        #region Public Members

        public bool IsNewItem { get; set; }

        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        public Guid ActiveParentId
        {
            get { return (hfParentId.Value != string.Empty) ? new Guid(hfParentId.Value) : Guid.Empty; }
            set { hfParentId.Value = value.ToString(); }
        }



        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;
            

            if (ParentId != Guid.Empty)
                ActiveParentId = ParentId;

            if (ActiveItemId != Guid.Empty)
            {
                bindItem();
            }
        }

        #endregion

        #region Event Handlers
        #endregion

        #region Helpers

        private void bindItem()
        {

            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(ActiveParentId);

            if (ci != null)
                lblHeaderRight.Text = ci.Name;
            
            SettingController sc = new SettingController();
            SettingInfo si = sc.SelectSetting(ActiveItemId);
            ConfigurationSettingController csc = new ConfigurationSettingController();
            if (si != null)
            {

                lblHeaderLeft.Text = "Configuration Setting - " + si.Name;
                lblSetting.Text = si.Name;
                if (si.Encrypted)
                    txtValue.TextMode = TextBoxMode.Password;
                else
                    txtValue.TextMode = TextBoxMode.SingleLine;

                ConfigurationSettingInfo csi = csc.SelectConfigurationSetting(ActiveParentId, si.SettingId);
                if (csi != null)
                {

                   

                    chkActive.Checked = csi.Active;
                    string value = csi.Value;
                    if (!si.Encrypted)
                    {
                        txtValue.Text = csi.Value;

                    }
                    txtPattern.Text = csi.Pattern;
                    txtReplacement.Text = csi.Replacement;
                    if (ddPermissionFlag.FindItemByValue(csi.PermissionFlag) != null)
                    {
                        ddPermissionFlag.FindItemByValue(csi.PermissionFlag).Selected = true;
                    } 
                    
                   
                }
            }
        }

        #endregion

        #region Action Event Helpers

        private void SaveItem()
        {
            RijndaelEnhanced re = new RijndaelEnhanced();

            if (Page.IsValid)
            {
                SettingController sc = new SettingController();
                SettingInfo si = sc.SelectSetting(ActiveItemId);
                string value = txtValue.Text;
                bool encrypted = false;

                if (si != null)
                {
                    if (si.Encrypted & value != string.Empty)
                    {
                        value = re.Encrypt(value);

                    }
                    encrypted = si.Encrypted;
                }

                ConfigurationSettingController csc = new ConfigurationSettingController();
                ConfigurationSettingInfo csi = csc.SelectConfigurationSetting(ActiveParentId, ActiveItemId);

                if (csi == null)
                {
                    csi = new ConfigurationSettingInfo();
                    csi.Value = value;
                    IsNewItem = true;
                }
                else
                {
                    if (encrypted)
                    {
                        string oldValue = csi.Value;
                        if (oldValue != string.Empty)
                            oldValue = re.Decrypt(csi.Value);

                        if (value != string.Empty && oldValue != re.Decrypt(value))
                        {
                                csi.Value = value;
                        }
                    }
                    else
                    {
                        csi.Value = value;
                    }
                }

                csi.ConfigurationId = ActiveParentId;
                csi.SettingId = ActiveItemId;
                csi.Active = chkActive.Checked;
                csi.PermissionFlag = ddPermissionFlag.SelectedValue;

                csi.Pattern = txtPattern.Text;
                csi.Replacement = txtReplacement.Text;

                if (IsNewItem)
                {
                    csc.InsertConfigurationSetting(csi);
                }
                else
                {
                    csc.UpdateConfigurationSetting(csi);

                }
                RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", ActiveParentId));
                csc = null;
            }


        }

        private void DeleteItem()
        {
            ConfigurationSettingController csc = new ConfigurationSettingController();
            csc.DeleteConfigurationSetting(ActiveParentId, ActiveItemId);
            csc = null;

            RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", ActiveParentId));
        }

        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveItem();
                    break;
                case "Delete":
                    DeleteItem();
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", ActiveParentId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Changes", "~/images/icons/save.png", "", true, true));
                al.Add(new ActionInfo("Delete", "Clear Setting", "~/images/icons/delete.png", "Are you sure you wish to clear this setting?", !IsNewItem));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));
                return al;
            }
        }

        #endregion
    }
}